import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import { GamePropData } from "../Data/GamePropData";
import MoveObj from "../Prefabs/InteractivObj/Common/MoveObj";
import Gate from "../Prefabs/InteractivObj/Gate";
import { utils } from "../../utils/CommonUtil";
import DialogDataConst from "../../constant/DialogDataConst";
import EventNameList from "../../constant/EventNameList";
import EnumData from "../../Enum/EnumData";
import GamePropConst from "../../constant/GamePropConst";

export default class LevelControl7 extends LevelBaseControl{
    

    //#region room1
    /** 可以移动的柜子*/
    private moveGuiZi:MoveObj;
    /** room1-room2门是否开了*/
    private isOpenRoom1_room2:boolean = false;
    /** 书里面的圆形火花塞*/
    private circleSpark:Laya.Sprite3D;
    /** 最终的大门1*/
    private finalGate1:Gate;
    /** 最终的大门2*/
    private finalGate2:Gate;
    //#endregion

    //#region room2    
    /** roo2_room3的门1*/
    private room2_room3_gate1:Gate;
    /** room2_room3门2*/
    private room2_room3_gate2:Gate;
    //#endregion

    //#region room3    
    /** room3_room2的门1*/
    private room3_room2_gate1:Gate;
    /** room3_room2的门2*/
    private room3_room2_gate2:Gate;
    /** room3_roo4的门*/
    private room3_room4_gate:Gate;
    //#endregion

    //#region room4
    /** room4_room3的门*/
    private room4_room3_gate:Gate;
    /** 柜子抽屉*/
    private chouTi:MoveObj;
    //#endregion

    protected startByBase(){
        this.InitRoom1();
        this.InitRoom2();
        this.InitRoom3();
        this.InitRoom4();
    }

    private InitRoom1()
    {
        let room1 = this.allLevelObjs[0].owner;
        let walls = room1.getChildByName("walls");
        let wall1 = walls.getChildByName("wall1");
        let wall3 = walls.getChildByName("wall3");

        let bookcase_2 = wall1.getChildByName("bookcase_2");
        this.moveGuiZi = utils.CommonUtils.CustomAddComponent(bookcase_2,MoveObj) as MoveObj;

        this.circleSpark = wall1.getChildByName("bookcase_1").getChildByName("book_01_props").getChildByName("Round key") as Laya.Sprite3D;

        let endGate1 = wall3.getChildByName("endGate1");
        let endGate2 = wall3.getChildByName("endGate2");

        this.finalGate1 = utils.CommonUtils.CustomAddComponent(endGate1,Gate) as Gate;
        this.finalGate2 = utils.CommonUtils.CustomAddComponent(endGate2,Gate) as Gate;
    }
    private InitRoom2()
    {
        let room2 = this.allLevelObjs[1].owner;
        let walls = room2.getChildByName("walls");
        let wall1 = walls.getChildByName("wall1");

        let room2_room3_Gate1 = wall1.getChildByName("room2_room3_Gate1");
        let room2_room3_Gate2 = wall1.getChildByName("room2_room3_Gate2");

        this.room2_room3_gate1 = utils.CommonUtils.CustomAddComponent(room2_room3_Gate1,Gate) as Gate;
        this.room2_room3_gate2 = utils.CommonUtils.CustomAddComponent(room2_room3_Gate2,Gate) as Gate;
    }

    private InitRoom3()
    {
        let room3 = this.allLevelObjs[2].owner;
        let walls = room3.getChildByName("walls");
        let wall1 = walls.getChildByName("wall1");
        let wall3 = walls.getChildByName("wall3");

        let room3_room2_Gate1 = wall3.getChildByName("room3_room2_Gate1");
        let room3_room2_Gate2 =  wall3.getChildByName("room3_room2_Gate2");
        let room3_room4_Gate = wall1.getChildByName("room3_room4_Gate");

        this.room3_room2_gate1 =utils.CommonUtils.CustomAddComponent(room3_room2_Gate1,Gate) as Gate;
        this.room3_room2_gate2 =utils.CommonUtils.CustomAddComponent(room3_room2_Gate2,Gate) as Gate;
        this.room3_room4_gate = utils.CommonUtils.CustomAddComponent(room3_room4_Gate,Gate) as Gate;
    }

    private InitRoom4()
    {
        let room4 = this.allLevelObjs[3].owner;
        let walls = room4.getChildByName("walls");
        let wall2 = walls.getChildByName("wall2");
        let wall3 = walls.getChildByName("wall3");

        let room4_room3_Gate = wall3.getChildByName("room4_room3_Gate");

        this.room4_room3_gate = utils.CommonUtils.CustomAddComponent(room4_room3_Gate,Gate) as Gate;
        let chouti = wall2.getChildByName("Drawer").getChildByName("chouti");
        
        this.chouTi = utils.CommonUtils.CustomAddComponent(chouti,MoveObj) as MoveObj;
    }
    public DoInteractObjLogic( _name:string):boolean
    {
        switch (_name)
        {
            /** coomon**************************************************************************/
            case "room2_room3_Gate1_Spark_Close":
            case "room2_room3_Gate2_Spark_Close":
            case "room3_room2_Gate2_Spark_Close":
            case "room3_room2_Gate1_Spark_Close":
            case "room3_room4_Gate_Spark_Close":
            case "room4_room3_Gate_Spark_Close":
                this.CloseGate(_name);            
            break;
            case "room2_room3_Gate1_Spark_Open":
            case "room2_room3_Gate2_Spark_Open":
            case "room3_room2_Gate2_Spark_Open":
            case "room3_room2_Gate1_Spark_Open":
            case "room3_room4_Gate_Spark_Open":
            case "room4_room3_Gate_Spark_Open":
                this.OpenGate(_name);
                break;
            /** room1**********************************************************************/
            /** 解锁物理密码锁 移动柜子*/
            case "password BOX_UnLock":
                this.ShowDialog(DialogDataConst.obj_be_moving);
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,this.PhysGuizi);
                this.isOpenRoom1_room2 = true;
            break;
            /** 柜子移动了 就能获得地上的火花塞*/
            case "Trapezoidal key":
                if (!this.isOpenRoom1_room2)
                {
                    return false;
                }
                break;
            /** 门开了 就进入room2*/
            case "room1-rom2Gate":
                if (this.isOpenRoom1_room2)
                {
                    if (this.cameraControl.curRoateY == 0)
                    {
                        this.ChangeCurLevelObj(this.allLevelObjs[1],EnumData.CameraMoveEff.RightToLeft);
                    }
                    else
                    {
                        this.ChangeCurLevelObj(this.allLevelObjs[1], EnumData.CameraMoveEff.LeftToRight);
                    }
                }
                break;
            /** 火花塞锁 解锁  最终的门打开*/
            case "SparkPlugLock_Spark_Unlock":
                this.finalGate1.SetOpenDirect();
                this.finalGate2.SetOpenDirect();
                this.ShowDialog(DialogDataConst.gate_open);
                break;
            /** 进入最终的门  过关 */
            case "endGate1_In_Gate":
            case "endGate2_In_Gate":
                this.FinishLevel();
                break;
            /** room2*************************************************************/
            /** 点击到room1的门*/
            case "room2_room1Gate":
                if (Math.abs(this.cameraControl.curRoateY) == 180)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[0],EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[0],EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            /** 点击到room2_room3的1门 进入*/
            case "room2_room3_Gate1_In_Gate":                
                if (Math.abs(this.cameraControl.curRoateY) == 0)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[2],EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[2],EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            /** 点击到room2_room3的2门 进入*/
            case "room2_room3_Gate2_In_Gate":
                if (Math.abs(this.cameraControl.curRoateY) == 0)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[2], EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[2], EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            /** room3************************************************************/
            /** 点击到room3_room2的1门 进入*/
            case "room3_room2_Gate1_In_Gate":
                if (Math.abs(this.cameraControl.curRoateY) == 180)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[1], EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[1], EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            /** 点击到room3_room2的2门 进入*/
            case "room3_room2_Gate2_In_Gate":
                if (Math.abs(this.cameraControl.curRoateY) == 180)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[1], EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this. ChangeCurLevelObj(this.allLevelObjs[1], EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            /** 点击room3_room4的门 进入*/
            case "room3_room4_Gate_In_Gate":                
                if (this.cameraControl.curRoateY == 0)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[3], EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[3], EnumData.CameraMoveEff.LeftToRight);
                }                
                break;
            /** room4****************************************************************/
            /** 点击room4_room3的门 进入*/
            case "room4_room3_Gate_In_Gate":
                if (Math.abs(this.cameraControl.curRoateY) == 180)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[2], EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[2], EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            /** 移动密码柜子 解锁成功*/
            case "MoveLock_MoveLock_Unlock":
                this.chouTi.Interactive();                
                break;
            default:
                break;
        }
        return true;
    }
    private PhysGuizi( obj:any)
    {
        window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,this.PhysGuizi)
        this.MoveGuiZi();
    }
    private MoveGuiZi()
    {
        this.moveGuiZi.Interactive();
    }
    onDestroy()
    {
        window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,this.PhysGuizi)
    }
    /** 根据门的名字 关闭对应的两扇门*/
    public CloseGate( gateName:string)
    {        
        switch (gateName)
        {
            case "room2_room3_Gate1_Spark_Close":
                let handler2 = ( obj:any) => {
                    this.room2_room3_gate1.SetClose();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler2)
                    this.ShowDialog(DialogDataConst.gate_closed);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler2);               
                this.room3_room2_gate1.SetCloseDirect();
                break;
            case "room3_room2_Gate1_Spark_Close":
                this.room2_room3_gate1.SetCloseDirect();
                let handler1 = ( obj) => {
                    this.room3_room2_gate1.SetClose();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler1)
                    this.ShowDialog(DialogDataConst.gate_closed);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler1);     
                break;
            case "room3_room4_Gate_Spark_Close":

                let handler3 = (obj:any) => {
                    this.room3_room4_gate.SetClose();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler3)
                    this.ShowDialog(DialogDataConst.gate_closed);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler3);  
                this.room4_room3_gate.SetCloseDirect();
                break;
            case "room4_room3_Gate_Spark_Close":

                let handler4 = (obj:any) => {
                    this.room4_room3_gate.SetClose();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler4)
                    this.ShowDialog(DialogDataConst.gate_closed);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler4);  
                this.room3_room4_gate.SetCloseDirect();                
                break;
            case "room2_room3_Gate2_Spark_Close":

                let handler5 = (obj:any) => {
                    this.room2_room3_gate2.SetClose();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler5)
                    this.ShowDialog(DialogDataConst.gate_closed);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler5);  
                this.room3_room2_gate2.SetCloseDirect();                
                break;
            case "room3_room2_Gate2_Spark_Close":

                let handler6 = (obj:any) => {
                    this.room3_room2_gate2.SetClose();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler6)
                    this.ShowDialog(DialogDataConst.gate_closed);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler6);  
                this.room2_room3_gate2.SetCloseDirect();
                break;                               
            default:
                break;
        }
    }
    /** 根据门的名字 开对应的两扇门*/
    public  OpenGate( gateName:string)
    {
        switch (gateName)
        {
            case "room2_room3_Gate1_Spark_Open":

                let handler1 = (obj:any) => {
                    this.room2_room3_gate1.SetOpen();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler1)
                    this.ShowDialog(DialogDataConst.gate_open);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler1);  
                this.room3_room2_gate1.SetOpenDirect();
                break;
            case "room3_room2_Gate1_Spark_Open":

                let handler2 = (obj:any) => {
                    this.room3_room2_gate1.SetOpen();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler2)
                    this.ShowDialog(DialogDataConst.gate_open);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler2);  
                this.room2_room3_gate1.SetOpenDirect();                
                break;
            case "room2_room3_Gate2_Spark_Open":

                let handler3 = (obj:any) => {
                    this.room2_room3_gate2.SetOpen();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler3)
                    this.ShowDialog(DialogDataConst.gate_open);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler3); 
                this.room3_room2_gate2.SetOpenDirect();
                break;
            case "room3_room2_Gate2_Spark_Open":

                let handler4 = (obj:any) => {
                    this.room3_room2_gate2.SetOpen();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler4)
                    this.ShowDialog(DialogDataConst.gate_open);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler4); 
                this.room2_room3_gate2.SetOpenDirect();
                break;                               
            case "room3_room4_Gate_Spark_Open":

                let handler5 = (obj:any) => {
                    this.room3_room4_gate.SetOpen();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler5)
                    this.ShowDialog(DialogDataConst.gate_open);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler5); 
                this.room4_room3_gate.SetOpenDirect();
                break;
            case "room4_room3_Gate_Spark_Open":

                let handler6 = (obj:any) => {
                    this.room4_room3_gate.SetOpen();
                    window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent, this,handler6)
                    this.ShowDialog(DialogDataConst.gate_open);
                };
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,handler6); 
                this.room3_room4_gate.SetOpenDirect();
                break;                              
            default:
                break;
        }
    }
    public GetGameProp( propData:GamePropData)
    {
        /** 如果获得的是圆形火花塞要把场景里的隐藏*/
        if (propData.id == GamePropConst.CIRCLE_SPARK_PLUG)
        {
            this.circleSpark.active = false;
        }
        super.GetGameProp(propData);
    }
}